/*
 *
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding UTF-8
 */
package net.comm.shequ.web.action;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import net.comm.oper.service.impl.NotAdvServiceImpl;
import net.comm.oper.web.form.NotAdvForm;
import net.comm.page.PageCtr;
import net.comm.shequ.service.impl.WuyeServiceImpl;
import net.comm.shequ.service.impl.WyMemberServiceImpl;
import net.comm.shequ.service.impl.WyServeServiceImpl;
import net.comm.shequ.service.impl.WyYanzhServiceImpl;
import net.comm.shequ.service.impl.YwMemberServiceImpl;
import net.comm.shequ.web.form.WuyeForm;
import net.comm.shequ.web.form.WyMemberForm;
import net.comm.shequ.web.form.WyServeForm;
import net.comm.shequ.web.form.WyYanzhForm;
import net.comm.shequ.web.form.YwMemberForm;
import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;
import net.comm.group.service.impl.GroupsServiceImpl;

import net.comm.user.service.impl.UserHomeServiceImpl;
import net.comm.user.web.form.UserHomeForm;
import net.comm.util.HtmlMessageBox;
import net.comm.web.base.AbstractBaseAction;


/**
 * 物业Action
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class WuyeAction extends AbstractBaseAction {
	
	private WuyeServiceImpl service = null;
	
	/**
	 * struct-config中该ACTION中的每个forward的常量定义
	 */
	final String LIST_FORWARD    = "list";
	final String OPERATE_FORWARD = "operate";
	final String HOME_FORWARD = "home";
	final String LOGIN_FORWARD = "loginFwd";		
	
	/**
	 *构造方法
	 */
	public WuyeAction()
	{
		super(false);
		
		/**
		 * 初始化业务组件接口
		 */
		service = new WuyeServiceImpl();
	}
	
	/**
	 * 默认动作
	 * 1.action值为空的时候,进入查询页面
	 * 2.每个操作都执行的代码,但是方法必须返回null
	 * @param mapping	 跳转的mapping值	
	 * @param request	 HttpServletRequest对象
	 * @param response	 HttpServletResponse对象
	 */
	public ActionForward startAction(ActionMapping mapping, ActionForm form,
									 HttpServletRequest request , 
									 HttpServletResponse response)
	{
		String action = getReqParam(request , ACTION_KEY);
		if(action == null || action.trim().length() == 0) {
			return mapping.findForward(LIST_FORWARD);
		} else {
			return null;
		}
	}
	
	/**
	 * 查询所有物业
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		WuyeForm theForm = (WuyeForm)form;
			
		// 生成分页对象
	
		theForm.setPage(this.getPageFromReq(request));
		
		ReturnMessage msg  = new ReturnMessage();
		
		/**
		 *调用services中的方法
		 */
		List list = service.list(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		/**
		 *设置列表的数据
		 */
		setAction(request, "list");
		request.setAttribute("list" , list);
		request.setAttribute("theForm", theForm);
		
		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , theForm.getPage());
		request.setAttribute(KEY , theForm.getKey());
		request.setAttribute(LAST_KEY , theForm.getLastKey());
	
		//取得动态评论
		
	
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(LIST_FORWARD);
	}
	/**
	 * 查询所有物业
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward adlist(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		WuyeForm theForm = (WuyeForm)form;
			
		// 生成分页对象
	
		theForm.setPage(this.getPageFromReq(request));
		
		ReturnMessage msg  = new ReturnMessage();
		
		/**
		 *调用services中的方法
		 */
		List list = service.adlist(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		/**
		 *设置列表的数据
		 */
		setAction(request, "list");
		request.setAttribute("list" , list);
		request.setAttribute("theForm", theForm);
		
		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , theForm.getPage());
		request.setAttribute(KEY , theForm.getKey());
		request.setAttribute(LAST_KEY , theForm.getLastKey());
	
		//取得动态评论
		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(LIST_FORWARD);
	}	
	/**
	 * 查询物业
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward home(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		WuyeForm theForm = (WuyeForm)form;
			
		// 生成分页对象
		theForm.setPage(this.getPageFromReq(request));
		
		ReturnMessage msg  = new ReturnMessage();
		//取得物业信息
		if(theForm.getWuyeID() > 0){
			service.get(theForm, msg);
			if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
			{
				HtmlMessageBox hmb = new HtmlMessageBox();
				hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
				hmb.setPrintMessage(msg.getMsg());
				hmb.setForwardUrl("javascript:history.go(-1)");
			}							
		}else {
			service.getbyVillageID(theForm, msg);
			if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
			{
				HtmlMessageBox hmb = new HtmlMessageBox();
				hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
				hmb.setPrintMessage(msg.getMsg());
				hmb.setForwardUrl("javascript:history.go(-1)");
				return showMsg(hmb , request , response , mapping);
			}					
		}

		request.setAttribute("theForm", theForm);
		
		//取得物业成员
		WyMemberServiceImpl memberservice = new WyMemberServiceImpl();
		WyMemberForm memberForm = new WyMemberForm();
		
		memberForm.setPage(this.getPageFromReq(request,100));
		memberForm.setQryWuyeID(theForm.getWuyeID());
		
		List memberlist = memberservice.adlist(memberForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}		
		request.setAttribute("memberlist" , memberlist);
		
		//取得业委公告  业委通知  留言板
		NotAdvServiceImpl notadvservice = new NotAdvServiceImpl();
		
		NotAdvForm notadvForm = new NotAdvForm();
		
		StringBuffer sb = new StringBuffer("");
		sb.append(GlbFinal.NTYPE_GONGGAO);
		sb.append(",");
		sb.append(GlbFinal.NTYPE_TONGZHI);
		
		notadvForm.setPage(this.getPageFromReq(request,5));
		notadvForm.setQryNtype(sb.toString());
		notadvForm.setQryNsort(GlbFinal.NSORT_WUYE);
		notadvForm.setQryNsortID(theForm.getWuyeID());
		
		List notadvlist = notadvservice.listbyGroup(notadvForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}		
		request.setAttribute("notadvlist" , notadvlist);
		
		//取得最新投诉 建议
		sb.delete(0, sb.length());
		
		sb.append(GlbFinal.NTYPE_TOUSU);
		sb.append(",");
		sb.append(GlbFinal.NTYPE_JIANYI);
		
		NotAdvForm notadvform = new NotAdvForm();
		
		notadvform.setPage(new PageCtr(1, 5, "N", " 1=1 "));
		notadvform.setQryNtype(sb.toString());
		notadvform.setQryNsort(GlbFinal.NSORT_WUYE);
		notadvform.setQryNsortID(theForm.getWuyeID());
		
		List liuyanlist = notadvservice.adlist(notadvform, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}			
		request.setAttribute("liuyanlist", liuyanlist);	

		//取得物业服务
		WyServeServiceImpl wyserveservice = new WyServeServiceImpl();
		
		WyServeForm wyserveForm = new WyServeForm();
		
		wyserveForm.setPage(new PageCtr(1, 5, "N", " 1=1 "));
		wyserveForm.setQryWuyeID(theForm.getWuyeID());
		
		List wyservelist = wyserveservice.list(wyserveForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}			
		request.setAttribute("wyservelist", wyservelist);			
		
		
		//如果当前浏览者是物业负责人，则取得待审核的验证申请列表
		Integer userID = (Integer)request.getSession().getAttribute(GlbFinal.SESSION_USERID);
		if(userID != null && userID == theForm.getChairman()){
			WyYanzhServiceImpl yanzhservice = new WyYanzhServiceImpl();
			WyYanzhForm yanzheForm = new WyYanzhForm();
			
			yanzheForm.setPage(new PageCtr(1, 25, "N", " 1=1 "));
			yanzheForm.setQryWuyeID(theForm.getWuyeID());
			yanzheForm.setQryStatus(GlbFinal.STS_CHECKING);
			
			List yanzhlist = yanzhservice.adlist(yanzheForm, msg);
			if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
			{
				HtmlMessageBox hmb = new HtmlMessageBox();
				hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
				hmb.setPrintMessage(msg.getMsg());
				hmb.setForwardUrl("javascript:history.go(-1)");
				return showMsg(hmb , request , response , mapping);
			}			
			request.setAttribute("yanzhlist", yanzhlist);	
			request.setAttribute("yznum", yanzheForm.getPage().getTotalNum());	
		}
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(HOME_FORWARD);
	}

	/**
	 * 查询物业
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward get(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		WuyeForm theForm = (WuyeForm)form;
			
		// 生成分页对象
		theForm.setPage(this.getPageFromReq(request));
		
		ReturnMessage msg  = new ReturnMessage();
		//取得物业信息
		service.get(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}		
		request.setAttribute("theForm", theForm);
		

		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(HOME_FORWARD);
	}
	
	/**
	 * 跳转到添加页面
	 */
	public ActionForward addPage(ActionMapping mapping, ActionForm form,
			 				 	 HttpServletRequest request , 
			 				 	 HttpServletResponse response)  
		throws Exception
	{
		WuyeForm theForm = (WuyeForm)form;
		theForm.setAction(getReqParam(request, "action"));
		
		request.setAttribute("theForm", theForm);
	
		return mapping.findForward(OPERATE_FORWARD);
	}
	
	/**
	 * 跳转到编辑页面
	 */
	public ActionForward modifyPage(ActionMapping mapping, ActionForm form,
							 	    HttpServletRequest request , 
							 	    HttpServletResponse response)
		throws Exception
	{
	
		return get(mapping , form, request , response);
	}
	
	/**
	 *添加新物业
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		WuyeForm theForm = (WuyeForm)form;

		ReturnMessage msg  = new ReturnMessage();
		
		service.add(theForm, msg);
		
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}

		return mapping.findForward(OPERATE_FORWARD);
	}

	
	/**
	 * 修改物业
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
							 	HttpServletRequest request , 
							 	HttpServletResponse response)
		throws Exception
	{

		WuyeForm theForm = (WuyeForm) form;

		ReturnMessage msg  = new ReturnMessage();
		
		service.update(theForm ,msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		return list(mapping , form, request , response);
	}


	/**
	 *删除单个物业
	 */
	public ActionForward del(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		WuyeForm theForm = (WuyeForm) form;
		
		ReturnMessage msg  = new ReturnMessage();
		String path = request.getSession().getServletContext().getRealPath("/");
		
		service.delete(theForm ,path, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}

		return list(mapping , form, request , response);
	}

	/**
	 * 中文英文切换页面
	 */
	public ActionForward tran(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response)
	{
		HttpSession session = request.getSession(true);
		String language = getReqParam(request , "language");
		
		if("en".equals(language)) {
			session.setAttribute(Globals.LOCALE_KEY , Locale.ENGLISH);
		} else {
			session.setAttribute(Globals.LOCALE_KEY , Locale.CHINESE);
		}
		
		return mapping.findForward("tran");
	}
	
	
	/**
	 * 
	 */
	public ActionForward dis(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage("msg.getMsg()");
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
	}	 

}
